package com.example.sign

import android.app.DatePickerDialog
import android.os.Bundle
import android.widget.DatePicker
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.entity.SignBeanItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.sign.databinding.ActivitySignBinding
import com.google.android.material.tabs.TabLayout
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.Calendar

@Route(path = "/sign/SignActivity")
@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignIntent, SignViewModel>() {
    override val binding by lazy {
        ActivitySignBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<SignViewModel>()
    val signAdapter by lazy { SignAdapter() }
    val list = mutableListOf<SignBeanItem>()
    var isStart=true
    override fun initView() {
        binding.signRv.apply {
            adapter = signAdapter
            layoutManager = LinearLayoutManager(this@SignActivity)
        }

        binding.signStatusTab.apply {
            addTab(newTab().setText("全部"), true)
            addTab(newTab().setText("正常"))
            addTab(newTab().setText("迟到"))
            addTab(newTab().setText("早退"))
            addTab(newTab().setText("缺勤"))
            getTabAt(0)!!.view.setBackgroundResource(R.drawable.shape_solid_blue)
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab!!.view.setBackgroundResource(R.drawable.shape_solid_blue)
                    signAdapter.apply {
                        data.clear()
                        if (tab.text != "全部") {
                            addData(list.filter { it.status == tab.text })
                        } else {
                            addData(list)
                        }
                        notifyDataSetChanged()
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab!!.view.setBackgroundResource(R.drawable.shape_solid_white)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
        }

        vm.sendIntent(SignIntent.getSigns(306/*SPUtils.getInstance().getInt(Const.UID,-1)*/))
        var startDate=""
        var endDate=""
        //按照日期范围筛查
        binding.signFilter.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : DatePickerDialog.OnDateSetListener {
                    override fun onDateSet(
                        view: DatePicker?,
                        year: Int,
                        month: Int,
                        dayOfMonth: Int
                    ) {
                        val m = if (month + 1 < 10) "0${month + 1}" else "${month + 1}"
                        val d = if (dayOfMonth < 10) "0${dayOfMonth}" else "${dayOfMonth}"

                        if(isStart){
                            startDate="${year}-${m}-${d}"
                            binding.signDate.setText(startDate)
                            isStart=!isStart
                        }else{
                            endDate="${year}-${m}-${d}"
                            binding.signDate.append("至${endDate}")
                            isStart=!isStart

                            signAdapter.apply {
                                data.clear()
                                addData(list.filter {
                                    if(it.morningCheckIn.isNotEmpty()){
                                        it.morningCheckIn.split(" ")[0] in startDate..endDate
                                    }else{
                                        it.afternoonCheckIn.split(" ")[0] in startDate..endDate
                                    }
                                })
                                notifyDataSetChanged()
                            }
                        }



                    }

                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        signAdapter.apply {
                            val items = it.data as Collection<SignBeanItem>
                            list.addAll(items.filter {
                                it.morningCheckIn.isNotEmpty() || it.afternoonCheckIn.isNotEmpty()
                            })
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }
                    else->{}
                }
            }
        }
    }

}